import { is_empty, obj_empty, loc } from 'sui';

const filterN = (main) => {
    if (obj_empty(main) || obj_empty(main.n)) {
        return null;
    }

    const bar = main.n.bar;
    const mul = main.n.mul;
    const norm = main.n.norm.map(d => [d[0], d[1] * mul]);

    const line = [norm];
    const name = [loc("zw_265")];

    if (!is_empty(main.n.gnorm)) {
        const tmp = main.n.gnorm.map(d => [d[0], d[1] * mul]);
        line.push(tmp);
        name.push(loc("zw_266"));
    }

    let x_min = norm[0][0];
    let x_max = norm[norm.length - 1][0];

    return {
        line: line,
        name: name,
        bar: bar,
        symbol: "no",
        x_min: x_min,
        x_max: x_max,
        xrange: true,
        hide_y: true,
    };
};

const filterG = (main) => {
    if (obj_empty(main) || obj_empty(main.g)) {
        return null;
    }

    const line = [];
    main.g.forEach((d, i) => d.forEach(t => line.push([i, t])));

    return {
        g: line,
        min: main.sta.min,
        max: main.sta.max,
    };
};

const sumSpc = (main, url) => {
    if (obj_empty(main)) {
        return [null, null];
    }

    const sta = main.sta;
    const cpk = main.cpk;

    const h1 = [loc("mg_006"), loc("mg_083"), loc("mg_006"), loc("mg_083")];
    const h2 = ["PP", loc("mg_083"), "CP", loc("mg_083")];

    const c2 = [
        [loc("zw_252"), to_f(sta, "std"), loc("zw_253"), to_f(sta, "gstd")],
        ["PP", to_f(cpk, "pp"), "CP", to_f(cpk, "cp")],
        ["PPU", to_f(cpk, "ppu"), "CPU", to_f(cpk, "cpu")],
        ["PPL", to_f(cpk, "ppl"), "CPL", to_f(cpk, "cpl")],
        ["Ppk", to_f(cpk, "ppk"), "Cpk", to_f(cpk, "cpk")],
    ];

    if (url === "xbar") {
        const c1 = [
            [loc("product_30"), main.total, loc("product_38"), main.row],
            [loc("product_18"), to_f(sta, "mean"), loc("zw_261"), to_f(sta, "mid")],
            [loc("mg_020"), to_f(sta, "max"), loc("product_48"), to_f(sta, "min")],
            [loc("zw_257"), to_f(sta, "var"), loc("product_47"), to_f(sta, "std")],
            [loc("zw_258"), to_f(sta, "skew"), loc("zw_262"), to_f(sta, "kurt")],
            [loc("zw_259"), to_f(sta, "cv"), loc("product_49"), to_f(sta, "ext")],
            ["5%" + loc("zw_260"), to_f(sta, "per5"), "95%" + loc("zw_260"), to_f(sta, "per95")],
            ["Q1" + loc("zw_260"), to_f(sta, "per25"), "Q3" + loc("zw_260"), to_f(sta, "per75")],
        ];

        return [h1, c1, h2, c2];
    }

    const data = main["d"];

    if (url === "p") {
        const c1 = [
            [loc("product_30"), main.total, loc("product_31"), data.reject],
            [loc("product_32"), (data.cl * 100).toFixed(0) + "%", loc("product_33"), data.group],
            [loc("product_34"), data.cl_val, loc("product_35"), data.max_val],
            [loc("product_36"), data.min_val, loc("product_49"), to_f(sta, "ext")],
            [loc("product_47"), to_f(sta, "std"), loc("product_18"), to_f(sta, "mean")],
            [loc("zw_155"), to_f(sta, "min"), loc("zw_156"), to_f(sta, "max")],
        ];

        return [h1, c1, h2, c2];
    }
    else if (url === "np") {
        const c1 = [
            [loc("product_30"), main.total, loc("product_31"), data.reject],
            [loc("product_32"), (data.cl * 100).toFixed(0) + "%", loc("product_33"), data.group],
        ];

        return [h1, c1, h2, c2];
    }
    else if (url === "c") {
        const c1 = [
            [loc("product_30"), main.total, loc("product_31"), data.reject],
            [loc("product_37"), (data.avg_reg * 100).toFixed(0) + "%", loc("product_33"), data.group],
        ];


        return [h1, c1, h2, c2];
    }
    else if (url === "u") {
        const c1 = [
            [loc("product_30"), main.total, loc("product_31"), data.reject],
            [loc("product_32"), (data.cl * 100).toFixed(0) + "%", loc("product_33"), data.group],
            [loc("product_36"), data.min_val, loc("product_49"), to_f(sta, "ext")],
            [loc("product_47"), to_f(sta, "std"), loc("product_18"), to_f(sta, "mean")],
            [loc("zw_155"), to_f(sta, "min"), loc("zw_156"), to_f(sta, "max")],
        ];

        return [h1, c1, h2, c2];
    }

    return [null, null, null, null];
};

const get_f = (d, k) => parseFloat(d[k]);

const to_f = (d, k, fix=true) => {
    if (!d) {
        return "";
    }

    const v = get_f(d, k);
    if (isNaN(v)) {
        return "";
    }

    if (fix) {
        return v.toFixed(4);
    }

    return v;
};

export {to_f, filterG, get_f, sumSpc, filterN};
